# Copyright (C) 2007 "dbsolution sas" 
#
# This file is part of Weebotee.
#
# Weebotee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Weebotee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Weebotee.  If not, see <http://www.gnu.org/licenses/>.

from django.template import Library, Node, NodeList
from libs import plug_core
from django.template.loader import get_template


register = Library()

class AdminMenuNode(Node):
    def __init__(self, template_path):
        self.link = [{"name": "content",
                      "title":"amministrazione contenuti",
                      "url":"/admin/contents/",
                      "icon": "/media/admin/icon-menu-content.gif" },
                     { "name" : "filebrowser",
                       "title":"amministrazione files",
                       "url":"/admin/filebrowser/",
                       "icon":"/media/admin/icon-menu-filebrowser.gif"},
                     {"name":"users",
                      "title":"amministrazione utenti",
                      "url": "/admin/users/",
                      "icon": "/media/admin/icon-menu-users.gif"},
                     {"name":"groups",
                      "title":"amministrazione gruppi",
                      "url":"/admin/groups/",
                      "icon": "/media/admin/icon-menu-groups.gif"}]
        
        self.elink = [{"name":"preview",
                       "title":"mostra sito",
                       "url":"/",
                       "icon": "/media/admin/icon-menu-preview.gif"},
                      {"name": "logout",
                       "title": "esci",
                       "url": "/accounts/logout",
                       "icon": "/media/admin/icon-menu-logout.gif"}
                      ]
        self.__dynControls()
        self.__template = get_template(template_path)

        
    def __dynControls(self):
        dynaLinks = plug_core.scan_applications("admin_link")
        #for l in dynaLinks.values():
        self.link.extend(dynaLinks.values())
        self.link.extend(self.elink)
                     
        
    def render(self, context):
        nodes = NodeList()
        for link in self.link:
            context["menulink"] = link 
            for node in self.__template:
                nodes.append(node.render(context))
        return nodes.render(context)

def menu_list(parser, token):
    return AdminMenuNode(template_path="widgets/admin_menu_bar.html")
register.tag('menu_list',menu_list)    
